import configparser
import datetime
import random

import pandas as pd
import uiautomator2 as u2

visit = 'visit08'

config = configparser.ConfigParser()
config.read('config.ini')
username = config.get(visit, 'username')
password = config.get(visit, 'password')
company = config.get(visit, 'company')
d = u2.connect()

d.app_start('cn.com.rektec.xmobile.jcy', wait=True)
list = []


def login():
    d(resourceId='cn.com.rektec.xmobile.jcy:id/edt_username').set_text(username)
    d(resourceId='cn.com.rektec.xmobile.jcy:id/edt_password').set_text(password)
    d(resourceId='cn.com.rektec.xmobile.jcy:id/chk_contract').click()
    d(resourceId='cn.com.rektec.xmobile.jcy:id/btn_login').click()
    d(text='n').click()
    d(text='医院拜访').click()
    # d(text='15').click()
    # d(text='签到地址：').click()


def date_choose():
    day = datetime.date.today().day
    for i in range(int(day / 7)):
        d(resourceId='rt-calendar-list').swipe('right')
        d.sleep(1)
    step = 0
    for j in list:
        date = d(text=j)
        if date.exists:
            date.click()
            d.sleep(1)
        else:
            swip(j, step)
            d.sleep(1)
            date.click()


def visit(day, user):
    df = pd.read_csv('input/chat.csv', encoding='utf8')
    df = df[(df['day'] == day) & (df['user'] == user)]
    for i, data in df.iterrows():
        visit = d(text=' 新增医生临时拜访')
        if visit.exists:
            visit.click()
        else:
            d.swipe_ext('up')
            d.swipe_ext('up')
            d.sleep(1)
            d.swipe_ext('up')
            d.swipe_ext('up')
            d.sleep(1)
            d(text=' 新增医生临时拜访').click()
        d.sleep(2)
        d(text='医生*').click()
        d.sleep(1)
        d(className="android.widget.EditText", instance=8).set_text(text=data['name'])
        d.sleep(1)
        d(resourceId='rt-search-font').click()
        d.sleep(1)
        d(text=data['name'], instance=1).click()
        d.sleep(1)
        d(text='甲方*').click()
        d.sleep(1)
        d(text=company).click()
        d.sleep(1)
        d(text='产品*').click()
        d.sleep(1)
        if (company == '医贸'):
            d(text=random.choice(['10ml:20mg*36瓶', '25ml:50mg*36瓶'])).click()
        else:
            d(text='尼洛替尼胶囊').click()
        d.sleep(1)
        d(text='拜访目的*').click()
        d.sleep(1)
        target = d(text=data['target'])
        if not target.exists:
            d.swipe_ext('up')
            d.swipe_ext('up')
            d.sleep(2)
            e = d.xpath('//android.view.View[@text="%s"]' % data['target'])
            e.click()
        else:
            target.click()
        d.sleep(1)
        d(text='确定').click()
        d.sleep(1)
        d(text='产品熟悉程度*').click()
        d.sleep(1)
        d(text=random.choice(['熟悉', '非常熟悉'])).click()
        d(resourceId='IsAssist').set_text(data['content'])
        d.sleep(1)
        d(text='保存').click()
    d(className="android.widget.Button").click()


def logout():
    button = d(className="android.widget.Button")
    button.click()
    d.sleep(2)
    button.click()
    d.sleep(2)
    d(text='我的').click()
    d(text='系统设置').click()
    d(text='退出登录').click()
    d(text='确定').click()


def swip(parm, step):
    swipe_max = 4
    if not d(text=parm).exists and step <= swipe_max:
        d(resourceId='rt-calendar-list').swipe('left')
        step += 1
        swip(parm, step)
    else:
        pass


if __name__ == '__main__':
    # login()

    day = 8  # 日期
    user = '德高百'  # 账号(人名)
    visit(day, user)

 